package com.cskaoyan.market.controller;


import com.cskaoyan.market.bo.TopicEditorBo;

import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.service.MarketTopicService;
import com.cskaoyan.market.service.MarketTopicServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.Arrays;

import java.util.List;

@WebServlet("/admin/topic/*")
@MultipartConfig
public class AdminTopicController extends HttpServlet {
    private MarketTopicService topicService = new MarketTopicServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/topic/", "");
        if("list".equals(op)){
            list(req,resp);
        }
        if("read".equals(op)){
            read(req,resp);
        }
    }

    private void read(HttpServletRequest req, HttpServletResponse resp) {
        String idParam = req.getParameter("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idParam);
        }catch (NumberFormatException e) {
            ResponseUtil.fail(404,"id不能为非正整数");
        }
        TopicEditorBo topicEditorBo = topicService.read(id);
        Object o = ResponseUtil.ok(topicEditorBo);
        try {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String title = req.getParameter("title");
        String subtitle = req.getParameter("subtitle");
        Integer page = null;
        Integer limit = null;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
        }catch (NumberFormatException e) {
            ResponseUtil.fail(404,"page和limit不能为非正整数");
        }
        List<MarketTopic> marketTopicList = topicService.list(page, limit, sort, order, title, subtitle);
        Object o = ResponseUtil.okList(marketTopicList);
        try {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/topic/", "");
        if("create".equals(op)){
            create(req, resp);
        }
        if("update".equals(op)){
            update(req, resp);
        }if("delete".equals(op)){
            delete(req,resp);
        }if("batch-delete".equals(op)){
            batchDelete(req,resp);
        }
    }

    private void batchDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String responseBody = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        List<String> ids = JacksonUtil.parseStringList(responseBody, "ids");
        System.out.println(ids);
        topicService.batchDelete(ids);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String responseBody = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketTopic marketTopic = objectMapper.readValue(responseBody, MarketTopic.class);
        topicService.delete(marketTopic.getId());
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String responseBody = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketTopic marketTopic = objectMapper.readValue(responseBody, MarketTopic.class);
        topicService.update(marketTopic);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));

    }
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String responseBody = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketTopic marketTopic = objectMapper.readValue(responseBody, MarketTopic.class);
        topicService.create(marketTopic);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }

}
